<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: ot_subtotal.php 399 2014-01-18 16:02:23Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 399 $
* 	zuletzt geaendert von:	$Author: siekiera $
* 	Datum:					$Date: 2014-01-18 16:02:23 +0000 (Sat, 18 Jan 2014) $
*
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
*
* 	Copyright (c) since 2011 SEO:mercari
* --------------------------------------------------------------------------------------
* 	based on:
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
include_once DIR_FS_INC.'inc.get_tax_rate.php';

class ot_subtotal {
  var $title, $output;

  function __construct() {
    global $price, $db;
    $this->code = 'ot_subtotal';
    $this->title = MODULE_ORDER_TOTAL_SUBTOTAL_TITLE;
    $this->description = MODULE_ORDER_TOTAL_SUBTOTAL_DESCRIPTION;
    $this->enabled = ((MODULE_ORDER_TOTAL_SUBTOTAL_STATUS == 'true') ? true : false);
    $this->sort_order = MODULE_ORDER_TOTAL_SUBTOTAL_SORT_ORDER;
    $this->output = array();
    $this->db = $db;
  }

  function process($values_from_cart = false, &$order_total_array) {
		global $order, $price;
		
		$order_total = $order_total_array['all'];
		$order_total['code']  = $this->code;
		$order_total['title'] = $this->title . ':';
		$order_total['sort_order'] = $this->sort_order;

    if ( $values_from_cart ) {
      $products = $_SESSION['cart']->products;
      $subtotal = 0;
      if (is_array($products)) {
        $order_total['netto'] = 0;
        $order_total['tax'] = array();
        $order_total['brutto'] = 0;

        foreach($products as $product) {
          if (!isset($order_total['tax'][$product['tax_class_id']]))
            $order_total['tax'][$product['tax_class_id']] = array(
                'id' => $product['tax_class_id'],
                'rate' => get_tax_rate($product['tax_class_id']),
                'value'=> 0);

          if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0) {
            $order_total_netto = $product['price'] * $product['quantity'];
            $order_total_brutto = $price->AddTax($product['price'], $order_total['tax'][$product['tax_class_id']]['rate']) * $product['quantity'];
          }
          else {
            $order_total_netto = $price->RemoveTax($product['price'], $order_total['tax'][$product['tax_class_id']]['rate']) * $product['quantity'];
            $order_total_brutto = $product['price'] * $product['quantity'];
          }

          $order_total['netto'] += $order_total_netto;
          $order_total['brutto'] += $order_total_brutto;
          $order_total['tax'][$product['tax_class_id']]['value'] += ($order_total_brutto - $order_total_netto);
        }
      }
    }
    else {
      $subtotal = $order->info['subtotal'];
    	global $order, $price;
    	echo_e('TODO', 'ECF');
/*
		if(MODULE_SMALL_BUSINESS_STATUS != 'true') {
			if($this->enabled == true) {
				reset($order->info['tax_groups']);
				while (list($key, $value) = each($order->info['tax_groups'])) {
					if ($value > 0) {

						if ($_SESSION['customers_status']['customers_status_show_price_tax'] != 0) {
						$this->output[] = array('title' => $key.':',
							                    'text' =>$price->format($value,true),
							                    'value' => $price->format($value, false));
						}
						if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0 && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {
						$this->output[] = array('title' => $key .':',
							                    'text' =>$price->format($value,true),
							                    'value' => $price->format($value, false));
						}
					}
				}
			}
		}
- */
    }

    if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0)
      $order_total['value'] = $order_total['netto'];
    else
      $order_total['value'] = $order_total['brutto'];

    $order_total['text'] = $price->format($order_total['value'], true);

		if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0
        && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 0) {
      $order_total['title'] = MODULE_ORDER_TOTAL_SUBTOTAL_TITLE_NO_TAX . ':';
		}
		
		if ($_SESSION['customers_status']['customers_status_show_price_tax'] == 0
        && $_SESSION['customers_status']['customers_status_add_tax_ot'] == 1) {

    }
    
		if ($_SESSION['customers_status']['customers_status_show_price_tax'] != 0) {

		}
		$order_total_array[$this->code] = $order_total;
		$this->output[] = $order_total;
  }

  function check() {
    if (!isset($this->_check)) {
      $check = $this->db->db_query("SELECT configuration_value from " . TABLE_CONFIGURATION . " WHERE configuration_key = 'MODULE_ORDER_TOTAL_SUBTOTAL_STATUS'");
      $this->_check = $check->_numOfRows;
    }

    return $this->_check;
  }

    function keys() {
      return array('MODULE_ORDER_TOTAL_SUBTOTAL_STATUS', 'MODULE_ORDER_TOTAL_SUBTOTAL_SORT_ORDER');
    }

    function install() {
      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, set_function, date_added) VALUES ('MODULE_ORDER_TOTAL_SUBTOTAL_STATUS', 'true', '6', '1','cfg_select_option(array(\'true\', \'false\'), ', now())");
      $this->db->db_query("INSERT INTO " . TABLE_CONFIGURATION . " (configuration_key, configuration_value, configuration_group_id, sort_order, date_added) VALUES ('MODULE_ORDER_TOTAL_SUBTOTAL_SORT_ORDER', '1', '6', '2', now())");
    }

    function remove() {
      $this->db->db_query("DELETE FROM " . TABLE_CONFIGURATION . " WHERE configuration_key in ('" . implode("', '", $this->keys()) . "')");
    }
  }
?>